package edu.msu.meldru20.practical;

import java.io.Serializable;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private static class Parameters implements Serializable {
		private static final long serialVersionUID = 10275539472837495L;
		public ArrayList<Integer> digitDisplay = new ArrayList<Integer>();
		public int stackPosition = 0;
	}
	
	private Parameters params = new Parameters();
	
	private TextView digits;
	
	public static final String PARAMS = "params";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        digits = (TextView) findViewById(R.id.textDigits);
        
        params.digitDisplay.add(Integer.valueOf(0));
        params.stackPosition = 0;
        
        if(savedInstanceState != null) {
        	params = (Parameters)savedInstanceState.getSerializable(PARAMS);
        	updateUI();
		}
    }


    /*@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }*/
    
    public void onClear(View view) {
    	params.digitDisplay.clear();
    	params.digitDisplay.add(Integer.valueOf(0));
    	params.stackPosition = 0;
    	updateUI();
    }
    
    public void onZero(View view) {
    	long ournumber = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString() + "0");
    	if(ournumber < Integer.MAX_VALUE) {
    		params.digitDisplay.remove(params.stackPosition);
    		params.digitDisplay.add(params.stackPosition, Integer.valueOf((int)ournumber));
    	}
    	updateUI();
    }
    
    public void onOne(View view) {
    	long ournumber = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString() + "1");
    	if(ournumber < Integer.MAX_VALUE) {
    		params.digitDisplay.remove(params.stackPosition);
    		params.digitDisplay.add(params.stackPosition, Integer.valueOf((int)ournumber));
    	}
    	updateUI();
    }
    
    public void onTwo(View view) {
    	long ournumber = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString() + "2");
    	if(ournumber < Integer.MAX_VALUE) {
    		params.digitDisplay.remove(params.stackPosition);
    		params.digitDisplay.add(params.stackPosition, Integer.valueOf((int)ournumber));
    	}
    	updateUI();
    }
    
    public void onThree(View view) {
    	long ournumber = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString() + "3");
    	if(ournumber < Integer.MAX_VALUE) {
    		params.digitDisplay.remove(params.stackPosition);
    		params.digitDisplay.add(params.stackPosition, Integer.valueOf((int)ournumber));
    	}
    	updateUI();
    }
    
    public void onEnter(View view) {
    	params.stackPosition++;
    	params.digitDisplay.add(Integer.valueOf(0));
    	updateUI();
    }
    
    public void onAdd(View view) {
    	if(params.stackPosition >= 1) {
	    	long newvalue = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString()) + 
	    			Long.parseLong(params.digitDisplay.get(params.stackPosition-1).toString());
	    	
	    	params.digitDisplay.remove(params.stackPosition);
	    	params.digitDisplay.remove(params.stackPosition-1);
	    	
	    	if(newvalue > Integer.MAX_VALUE)
	    		params.digitDisplay.add(params.stackPosition-1, Integer.MAX_VALUE);
	    	else
	    		params.digitDisplay.add(params.stackPosition-1, Integer.valueOf((int)newvalue));
	    	
	    	params.stackPosition--;
    	}
    	updateUI();
    }
    
    public void onMultiply(View view) {
    	if(params.stackPosition >= 1) {
	    	long newvalue = Long.parseLong(params.digitDisplay.get(params.stackPosition).toString()) *
	    			Long.parseLong(params.digitDisplay.get(params.stackPosition-1).toString());
	    	
	    	params.digitDisplay.remove(params.stackPosition);
	    	params.digitDisplay.remove(params.stackPosition-1);
	    	
	    	if(newvalue > Integer.MAX_VALUE)
	    		params.digitDisplay.add(params.stackPosition-1, Integer.MAX_VALUE);
	    	else
	    		params.digitDisplay.add(params.stackPosition-1, Integer.valueOf((int)newvalue));
	    	
	    	params.stackPosition--;
    	}
    	updateUI();
    }
    
    public void updateUI() {
    	if(params.digitDisplay.get(params.stackPosition).equals(Integer.MAX_VALUE))
    		digits.setText(R.string.overflow);
    	else
    		digits.setText(params.digitDisplay.get(params.stackPosition).toString());
    }
    
    @Override
	protected void onSaveInstanceState(Bundle bundle) {
		super.onSaveInstanceState(bundle);
		
		bundle.putSerializable(PARAMS, params);
	}
    
}
